package com.snow.useviewdemo.function2.printer;

import android.content.Intent;
import android.content.res.AssetManager;
import android.graphics.Typeface;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.TypedValue;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.snow.useviewdemo.R;
import com.snow.useviewdemo.base.BaseActivity;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by zhaoyingtao
 * Date: 2021/3/19
 * Describe:
 */
public class PrinterSettingActivity extends BaseActivity {
    @BindView(R.id.et_content)
    EditText etContent;
    @BindView(R.id.et_speed_time)
    EditText etSpeedTime;
    @BindView(R.id.et_text_size)
    EditText etTextSize;
    @BindView(R.id.et_bg_color)
    EditText etBgColor;
    @BindView(R.id.et_text_color)
    EditText etTextColor;
    @BindView(R.id.textViewSize)
    TextView textViewSize;
    @BindView(R.id.textStyle01)
    TextView textStyle01;
    @BindView(R.id.textStyle02)
    TextView textStyle02;
    @BindView(R.id.textStyle03)
    TextView textStyle03;
    @BindView(R.id.btn_go)
    Button btnGo;
    PrinterIntentBean intentBean;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_printer_setting;
    }

    @Override
    protected void setViewData(Bundle savedInstanceState) {
        intentBean = new PrinterIntentBean();
        intentBean.setSpeedTime(200);
        intentBean.setTextStyle(1);
        intentBean.setTextSize(26);
        textStyle01.setText("方正硬行体");
        textStyle02.setText("华文行体");
        textStyle03.setText("华文新魏");
        setTextStyle(textStyle01, 1);
        setTextStyle(textStyle02, 2);
        setTextStyle(textStyle03, 3);
        selectTextStyle(textStyle01);
        editTextListener();
    }

    private void editTextListener() {
        etTextSize.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (!TextUtils.isEmpty(editable.toString())) {
                    int size = Integer.parseInt(editable.toString());
                    intentBean.setTextSize(size);
                    textViewSize.setTextSize(TypedValue.COMPLEX_UNIT_DIP, size);
                }
            }
        });
        etBgColor.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (!TextUtils.isEmpty(editable.toString())) {
                    intentBean.setBgColor(editable.toString());
                }
            }
        });
        etTextColor.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (!TextUtils.isEmpty(editable.toString())) {
                    intentBean.setTextColor(editable.toString());
                }
            }
        });
    }

    @OnClick({R.id.textStyle01, R.id.textStyle02, R.id.textStyle03, R.id.btn_go})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.textStyle01:
                intentBean.setTextStyle(1);
                selectTextStyle(textStyle01);
                break;
            case R.id.textStyle02:
                intentBean.setTextStyle(2);
                selectTextStyle(textStyle02);
                break;
            case R.id.textStyle03:
                intentBean.setTextStyle(3);
                selectTextStyle(textStyle03);
                break;
            case R.id.btn_go:
                if (!TextUtils.isEmpty(etContent.getText())) {
                    intentBean.setContent(etContent.getText().toString());
                }
                if (!TextUtils.isEmpty(etSpeedTime.getText())) {
                    intentBean.setSpeedTime(Integer.parseInt(String.valueOf(etSpeedTime.getText())));
                }
                if (!TextUtils.isEmpty(intentBean.getContent())) {
                    Intent intent = new Intent(PrinterSettingActivity.this, PrinterStartActivity.class);
                    intent.putExtra("intentBean", intentBean);
                    startActivity(intent);
                } else {
                    Toast.makeText(this, "请输入内容", Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }

    private void selectTextStyle(TextView textView) {
        textStyle01.setBackgroundColor(getResources().getColor(R.color.transparent));
        textStyle02.setBackgroundColor(getResources().getColor(R.color.transparent));
        textStyle03.setBackgroundColor(getResources().getColor(R.color.transparent));

        textView.setBackgroundResource(R.drawable.bg_white_fill_gray_frame_fillet_15);
    }

    private void setTextStyle(TextView textView, int style) {
        AssetManager mgr = getAssets();//得到AssetManager
        String textFont = "fonts/fangzheng_ying_xingkai.TTF";
        ;
        if (style == 1) {
            textFont = "fonts/fangzheng_ying_xingkai.TTF";
        } else if (style == 2) {
            textFont = "fonts/huawen_xingkai.ttf";
        } else if (style == 3) {
            textFont = "fonts/huawen_xinwei.TTF";
        }
        Typeface tf = Typeface.createFromAsset(mgr, textFont);//根据路径得到Typeface
        textView.setTypeface(tf);//设置字体
    }
}
